package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import bean.Driver;
import bean.Manager;
import tools.DBTools;
public class DriverDaoImpl implements DriverDao{

    //与数据库建立连接
    Connection conn = DBTools.getConn();
    PreparedStatement pstmt = null;
    @Override
    public boolean userlogin(String username, String upassword) {
        // TODO Auto-generated method stub
        boolean flag =false;
        String sql = "select * from UserTable where uname=? and upwd=?";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, upassword);
            ResultSet rs=pstmt.executeQuery();
            if(rs.next()){
                flag=true;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public Manager managerlogin(String mname) {
        // TODO Auto-generated method stub
        Manager mg = new Manager();
        String sql = "select * from ManagerTable where mname=?";
        try {
            pstmt=conn.prepareStatement(sql);
            mg.setMname(mname);
            pstmt.setString(1, mname);
            ResultSet rs=pstmt.executeQuery();
            while(rs.next()){
                String mpwd=rs.getString(2);
                mg.setMpwd(mpwd);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return mg;
    }

    @Override
    public int addDriver(Driver driver) {
        // TODO Auto-generated method stub
        Connection conn=DBTools.getConn();
        int i=0;
        String sql="insert into driverinfortable(did,dname,dsex,dbirth,daddress,dtype,dnum,dusetime) values(?,?,?,?,?,?,?,?)";
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, driver.getDid());
            pstmt.setString(2, driver.getDname());
            pstmt.setString(3, driver.getDsex());
            pstmt.setString(4, driver.getDbirth());
            pstmt.setString(5, driver.getDaddress());
            pstmt.setString(6, driver.getDtype());
            pstmt.setString(7, driver.getDnum());
            pstmt.setString(8, driver.getDusetime());

            i=pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            DBTools.close(conn, pstmt, null);
        }
        return i;
    }

    @Override
    public int updateDriver(Driver driver) {

        // TODO Auto-generated method stub
        Connection conn=DBTools.getConn();
        int i=0;
        String sql="update driverinfortable set dname=?,dsex=?,dbirth=?,daddress=?,dtype=?,dnum=?,dusetime=? where did=?";
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);

            pstmt.setString(1, driver.getDname());
            pstmt.setString(2, driver.getDsex());
            pstmt.setString(3, driver.getDbirth());
            pstmt.setString(4, driver.getDaddress());
            pstmt.setString(5, driver.getDtype());
            pstmt.setString(6, driver.getDnum());
            pstmt.setString(7, driver.getDusetime());
            pstmt.setInt(8, driver.getDid());
            i=pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            DBTools.close(conn, pstmt, null);
        }
        return i;
    }

    @Override
    public int deleteDriver(int did) {
        // TODO Auto-generated method stub
        Connection conn=DBTools.getConn();
        int i=0;
        String sql = "delete from driverinfortable where did=?";
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, did);
            i = pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            DBTools.close(conn, pstmt, null);
        }
        return i;
    }



    @Override
    public List<Driver> getDriverBydid(int did) {
        // TODO Auto-generated method stub
        String sql="select * from driverinfortable where did=?";
        List<Driver> driverList = null;
        try {
            pstmt = conn.prepareStatement(sql);
            //传参数
            pstmt.setInt(1, did);
            ResultSet rs=pstmt.executeQuery();
            driverList = new ArrayList<Driver>();
            Driver driver = null;
            while(rs.next()) {
                driver = new Driver();
                driver.setDid(rs.getInt(1));
                driver.setDname(rs.getString(2));
                driver.setDsex(rs.getString(3));
                driver.setDbirth(rs.getString(4));
                driver.setDaddress(rs.getString(5));
                driver.setDtype(rs.getString(6));
                driver.setDnum(rs.getString(7));
                driver.setDusetime(rs.getString(8));
                driverList.add(driver);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return driverList;
    }



    @Override
    public String[] getColumnNames(String tableName) {
        // TODO Auto-generated method stub
        String[] columnNames = null;

        String sql = "select * from " + tableName;

        try {
            pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();

            columnNames = new String[rsmd.getColumnCount()];
            for (int i = 0; i < columnNames.length; i++) {
                columnNames[i] = rsmd.getColumnLabel(i + 1);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return columnNames;
    }

    @Override
    public List<Driver> getDriverbySth() {
        // TODO Auto-generated method stub
        List<Driver> driverList = null;
        String sql = "select * from driverinfortable";
        try {
            pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            driverList = new ArrayList<Driver>();
            Driver driver = null;
            while(rs.next()) {
                driver = new Driver();
                driver.setDid(rs.getInt(1));
                driver.setDname(rs.getString(2));
                driver.setDsex(rs.getString(3));
                driver.setDbirth(rs.getString(4));
                driver.setDaddress(rs.getString(5));
                driver.setDtype(rs.getString(6));
                driver.setDnum(rs.getString(7));
                driver.setDusetime(rs.getString(8));
                driverList.add(driver);	//将这条记录添入列表
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return driverList;
    }




}